<?php 
//session_start();
include('header.php'); 
?>
<!doctype>
<!DOCTYPE html>
<html>
<head>
	<title>Register Customer</title>
</head>
<body>	
	<div class="container">
	<h2>Customer Registration</h2>
	<?php
			$errors = 0;
			$email = "";
			if (empty($_POST['email'])) 
			{
				++$errors;
				echo "<p>You need to enter an e-mail address.</p>\n";
			}
			else 
			{
				$email = stripslashes($_POST['email']);
				if (preg_match("/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\._-] +)+$/" , $email))
				{
					++$errors;
					echo "<p>You need to enter a valid " .
					"e-mail address.</p>\n";
					$email = "";
			    }
			}

			if (empty($_POST['password'])) 
			{
				++$errors;
				echo "<p>You need to enter a password.</p>\n";
				$password = "";
			}else
				$password = stripslashes($_POST['password']);

			if (empty($_POST['password2'])) 
			{
				++$errors;
				echo "<p>You need to enter a confirmation
				password.</p>\n";
				$password2 = " ";
			}else
				$password2 = stripslashes($_POST['password2']);
			
			if ((!(empty($password))) && (!(empty($password2))))
			{
				if (strlen($password) < 6) 
				{
					++$errors;
					echo "<p>The password is too short.</p>\n";
					$password = "";
					$password2 = "";
				}
				if ($password <> $password2) 
				{
					++$errors;
					echo "<p>The passwords do not match.</p>\n";
					$password = "";
					$password2 = "";
				}
			}
			
			if ($errors == 0) 
			{
				$DBConnect = @mysql_connect("localhost", "root","");

				if ($DBConnect === FALSE) 
				{
					echo "<p>Unable to connect to the database server. " ."Error code " . mysql_errno() . ": " .
						mysql_error() . "</p>\n";
						++$errors;
				}
				else
				{
					$DBName = "online_stores";
					$result = @mysql_select_db($DBName,
								$DBConnect);
					if ($result === FALSE) 
					{
						echo "<p>Unable to select the
								database. " .
									"Error code " . mysql_errno($DBConnect) .
									": " . mysql_error($DBConnect) .
									"</p>\n";
								++$errors;
					}
				}
			}

			$TableName = "customer";
			if ($errors == 0) 
			{
				$SQLstring = "SELECT count(*) FROM $TableName" .
									"where email = '$email'";
				$QueryResult = @mysql_query($SQLstring,	$DBConnect);
				if ($QueryResult !== FALSE) 
				{
					$Row = mysql_fetch_row($QueryResult);
					if ($Row[0]>0) 
					{
						echo "<p>The email address entered (" .
								htmlentities($email) .
									") is already registered.</p>\n";
						++$errors;
					}
				}
			}

			if ($errors > 0) 
			{
				echo "<p>Please use your browser's BACK button to return" .
						" to the form and fix the errors indicated.</p>\n";
			}

			/*if ($errors == 0) 
			{
				$first = stripslashes($_POST['first']);
				$last = stripslashes($_POST['last']);
				$address = stripcslashes($_POST['address']);
				$username = stripcslashes($_POST['username']);
				//$credit = 1000;
				$SQLstring = "INSERT INTO $TableName " .
					" (username, firstName, lastName, address, email , password_md5) ".
					" VALUES('$username', '$first', '$last', '$address', '$email', '" . md5($password) . "')";
				$QueryResult = mysql_query($SQLstring, $DBConnect);

				if ($QueryResult === FALSE) 
				{
					echo "<p>Unable to save your registration " .
						" information. \nError code " .
							mysql_errno($DBConnect) . ": " .
								mysql_error($DBConnect) . "</p>\n";
					++$errors;
				}
				else{
					$customerID = mysql_insert_id($DBConnect);
				}
			    mysql_close($DBConnect);
			}*/
			
			if ($errors == 0){				
				//$customerName = $first . " " . $last;
				echo "<p>Your Password has successfully been changed!\n";
				
			}

			//hidden input
			if ($errors == 0) {
				# code...
				echo "<form method='post' action='index.php'>\n";
				echo "<input type='hidden' name='customerID' value='$customerID'>\n";
				echo "<input type='submit' name='submit' value='View Tees For Africa products'>\n";
				echo "</form>";
			}
?>
<button type="submit" name="index.php" class="btn btn-primary">Continue looking at items...</button>
</div>
</body>
</html>

<?php include('footer.php'); ?>